﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Framework;

namespace PlanningGIS.Entry
{
    /// <summary>
    /// 动画窗体
    /// </summary>
    public partial class FrmSplash : Form
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Text"></param>
        delegate void StringParameterDelegate(string Text);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="tom"></param>
        delegate void StringParameterWithStatusDelegate(string Text, TypeOfMessage tom);

        /// <summary>
        /// 
        /// </summary>
        delegate void SplashShowCloseDelegate();

        /// <summary>
        /// To ensure splash screen is closed using the API and not by keyboard or any other things
        /// </summary>
        bool CloseSplashScreenFlag = false;

        /// <summary>
        /// Base constructor
        /// </summary>
        public FrmSplash()
        {
            InitializeComponent();

            //使控件拥有透明背景色
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.lblTitle.Parent = this.pictureBox1;
            this.lblTitle.BackColor = Color.Transparent;

            this.lblCopyRight.Parent = this.pictureBox1;
            this.lblCopyRight.BackColor = Color.Transparent;

            string sname = System.Configuration.ConfigurationManager.AppSettings.Get("SysName");
            //如果用自定义的闪屏界面，则文字不显示
            if (Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings.Get("CustomSplashScreen")))
            {
                lblTitle.Text = "";
                lblCopyRight.Text = "";
            }
            else
            {
                lblTitle.Text = sname;
            }
            string strImage = string.Format("{0}\\{1}.gif", AppConfig.ImagePath, sname);
            if (!System.IO.File.Exists(strImage))
                strImage = string.Format("{0}\\Splash.gif", AppConfig.ImagePath);
            if (System.IO.File.Exists(strImage))
            {
                try
                {
                    pictureBox1.Image = Image.FromFile(strImage);
                }
                catch {
                }
            }
        }

        /// <summary>
        /// Displays the splashscreen
        /// </summary>
        public void ShowSplashScreen()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new SplashShowCloseDelegate(ShowSplashScreen));
                return;
            }
            this.Show();
            Application.Run(this);
        }

        /// <summary>
        /// Closes the SplashScreen
        /// </summary>
        public void CloseSplashScreen()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new SplashShowCloseDelegate(CloseSplashScreen));
                return;
            }
            CloseSplashScreenFlag = true;
            this.Close();
        }

        /// <summary>
        /// Update text in default green color of success message
        /// </summary>
        /// <param name="Text">Message</param>
        public void UdpateStatusText(string Text)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new StringParameterDelegate(UdpateStatusText), new object[] { Text });
                return;
            }
            lblTitle.Text = Text;
        }
        
        /// <summary>
        /// Update text with message color defined as green/yellow/red/ for success/warning/failure
        /// </summary>
        /// <param name="Text">Message</param>
        /// <param name="tom">Type of Message</param>
        public void UdpateStatusTextWithStatus(string Text, TypeOfMessage tom)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new StringParameterWithStatusDelegate(UdpateStatusTextWithStatus), new object[] { Text, tom });
                return;
            }            
            lblTitle.Text = Text;
        }

        /// <summary>
        /// Prevents the closing of form other than by calling the CloseSplashScreen function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SplashForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (CloseSplashScreenFlag == false)
                e.Cancel = true;
        }
    }
}
